<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改密码</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- 引入样式 -->
    <!--<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">-->
    <link rel="stylesheet" href="../css/theme-chalk.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">

    <!-- 上传图片框样式，不使用上传组件可以删除此样式 -->
    <style>






        .el-container:nth-child(5) .el-aside,
        .el-container:nth-child(6) .el-aside {
            line-height: 260px;
        }

        .el-container:nth-child(7) .el-aside {
            line-height: 320px;
        }



        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 150px;
            height: 100px;
            line-height: 100px;
            text-align: center;
        }

        .avatar {
            width: 150px;
            height: 150px;
            display: block;
        }

    </style>
</head>
<body class="hold-transition">
<div id="app">

    <div class="content-header">
        <h1>管理
            <small>添加管理员</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>管理</el-breadcrumb-item>
            <el-breadcrumb-item>账户设置</el-breadcrumb-item>
        </el-breadcrumb>
    </div>


    <div class="app-container" align="center">
        <div class="box" style="width: 300px">
            <div class="filter-container" >

                <el-container>
                    <el-header>添加管理员</el-header>
                    <el-main>

                        <el-form :model="pojo" status-icon :rules="rules2" ref="editorForm" label-width="100px" class="demo-ruleForm">


                            <el-form-item label="用户名">
                                <el-input type="text" v-model="pojo.loginName"></el-input>
                            </el-form-item>

                            <el-form-item label="选择角色">
                                <el-select v-model="pojo.selectedRoles" multiple placeholder="请选择">
                                    <el-option
                                            v-for="item in roles"
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>




                                <el-form-item label="登录密码" >
                                    <el-input type="password" v-model="pojo.pass" auto-complete="off"></el-input>
                                </el-form-item>
                                <el-form-item label="确认密码" prop="checkPass">
                                    <el-input type="password" v-model="pojo.checkPass" auto-complete="off"></el-input>
                                </el-form-item>




                            <el-form-item>
                                <el-button type="primary" @click="operate()">提交</el-button>

                            </el-form-item>


                        </el-form>

                    </el-main>
                </el-container>




            </div>
        </div>
    </div>


</div>
</body>
<script src="/js/vue.js"></script>
<script src="/js/axios.js"></script>
<script src="/js/elementui.js"></script>
<script src="/js/util.js"></script>
<script>
    new Vue({
        el:'#app',

        data(){
            /*let validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.pojo.checkPass !== '') {
                        this.$refs.pojo.validateField('checkPass');
                    }
                    callback();
                }
            };
            let validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.pojo.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };*/
            return{
                pojo: {
                    loginName:'',
                    selectedRoles:[],
                    pass: '',
                    checkPass: ''
                },
               /* rules2: {
                    pass: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPass: [
                        {validator: validatePass2, trigger: 'blur'}
                    ]
                },*/
               id:'',
                tableData:[],
                roles:[],


                adminAndRoleId:{}
            }
        },
        created(){
            this.id=getQueryString("id");
            this.fetchData();
        },
        methods:{
            fetchData(){
                axios.get(`/role/findAll.do`).then(response=>{
                    this.roles=response.data;

                });
                //回显roles
                if (this.id != null){
                    axios.get(`/adminAndRoleId/findAdminAndRoleIdById.do?id=${this.id}`).then(response=>{
                        this.adminAndRoleId=response.data;
                        this.pojo.selectedRoles=this.adminAndRoleId.listId;
                        this.pojo.loginName=this.adminAndRoleId.admin.loginName;
                        this.pojo.pass=this.adminAndRoleId.admin.password;
                        this.pojo.pass=null;
                    });
                }
            },
            operate(){
                axios.post(`/admin/${this.id == null ? 'add' : 'update'}.do?id=${this.id}`,this.pojo).then(response=>{
                    if(response.data.code==0){
                        alert(response.data.message);
                        location.href="/system/admin.html"
                    }else {
                        alert(response.data.message);
                        window.location.reload();
                    }
                });
            }

        }
    })

</script>
</html>
